grid_w = 40
grid_h = 40
def get_target():
    targets = []
    for i in range(-3,3):
        for j in range(-3,3):
            if i == 0 or j== 0:
                continue
            targets.append((i,j))
    return targets

def get_random_target(botx_grid,boty_grid):
    targets = get_target()
    l = len(targets)
    targetx, targety= targets[int(random(0,l))]        
    target  =botx_grid+ targetx,boty_grid+ targety
    print('target x y',targetx, targety)
    return target
def get_bot_power(bot):
    return bot['player_state']['obj']['power']
def make_map(position_brick):
    
    for row in range(4, 11, 2):
        #if row ==6 or row == 8 or row == 10:continue
        for col in range(5, 7, 2):
            position_brick['gongfu'].append((col, row))
    # for x,y in  [(5,5),(7,5),(9,5),(11,5)]:
    #     position_brick['gongfu'].append((x, y))
    # return  
    
    # for row in range(0, 13):
    #     for col in range(0, 15):
    #         x,y = (7,6)
    #         if abs(x - col) + abs(y-row) == 6:
    #             position_brick['gongfu'].append((col, row))
    
def get_grid_centre(row,col):
    return row*grid_h+ 0.5*grid_h, col * grid_w + 0.5 * grid_w

def get_grid_topleft(row,col):
    return row*grid_h   , col * grid_w  

def get_grid_idx(x,y):
    return  int(x/grid_w),int(y/grid_h)

def is_neighbood(p1,p2):
    x,y = p1
    x2,y2 = p2
    if x== x2 and abs(y-y2) ==1:
        return True
    if y==y2 and abs(x-x2)==1:
        return True
    return False


def is_at_boom_range(p1,p2):
    x,y = p1
    x2,y2 = p2
    return x==x2 or y == y2

def find_places(boom_p,r):
    x,y = boom_p
    xs  = [x]
    for i in range(r+1):
        xs.append(x+i)
        xs.append(x-i)
        
    ys  = [y]
    for i in range(r+1):
        ys.append(y+i)
        ys.append(y-i)
    ys = list(set(ys))
    xs = list(set(xs))
    res = []
    for x in xs:
        for y in ys:
            res.append((x,y))
    return res

def find_boom_place(center,r,bubble_time):
    res = find_places(center,r)
    booms = []
    for p in res:
        if p in bubble_time:
            booms.append(p)
    return booms


def find_obj_place(center, r, position_object):
    res = find_places(center, r)
    booms = []
    for p in res:
        for pos_es in position_object.values():
            if p in pos_es:
                booms.append(p)
    return booms
            
def find_safe_place(boom_p,r):
    res = find_places(boom_p,r)
    safes = []
    #print({'search_rage':res})
    for p in res:
        if not is_at_boom_range(p,boom_p):
            safes.append(p)
    #print({'search_safes':safes})
    return safes


def find_attrack_place(boom_p,r):
    res = find_places(boom_p,r)
    att = []
    for p in res:
        if  is_at_boom_range(p,boom_p):
            att.append(p)
    return att

def in_bot_attract_range(boom_p,r,target):
    a = find_attrack_place(boom_p,r)
    if isinstance(a,list):
        if target in a:
            return True
    return False
        
def find_nearest_safe(bot_p , boom_p,r):
    safes = find_safe_place(boom_p,r)
    botx,boty = bot_p
    safes = sorted(safes,key = lambda x:abs(x[0] - botx)+ abs(x[1] - boty))
    return safes

def find_nearest_attrack(target_bot_p , boom_p,r):
    safes = find_attrack_place(boom_p,r)
    botx,boty = target_bot_p
    safes = sorted(safes,key = lambda x:abs(x[0] - botx)+ abs(x[1] - boty))
    return safes



def find_nearest_obj(center , r,position_object):
    safes = find_obj_place(center,r,position_object)
    botx,boty = center
    safes = sorted(safes,key = lambda x:abs(x[0] - botx)+ abs(x[1] - boty))
    return safes
    

def new_obj(position_object,p):
    r = random(0,3)
    ks = list(position_object.keys())
    objname = ks[int(r)]
    #p = get_grid_idx(random(0,w),random(0,h))
    position_object[objname].append(p)


def try_hit_brick(p,white_bricks,position_object):
    if p in white_bricks:
        white_bricks.remove(p) 
        print('hit brick')
        new_obj(position_object,p)
        return True
    return False

def is_hit_brick(p,white_bricks,position_object):
    if p in white_bricks:
       
        return True
    return False



#position_object = {'number':[],'speed':[],'power':[]}

def get_inverted_obj_map(position_object):
    d = {}
    for k ,ps in position_object.items():
        for p in ps:
            d[p] = k
    return d

def is_player_on_obj(pV,obj_map):
    player_p= get_grid_idx(pV.x,pV.y)
    return  obj_map.get(player_p),player_p
        

        